<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use DB;
use Image;
use Storage;
use App\photo;
use App\Http\Requests;
use App\Http\Controllers\Controller;
use App\xiang;
class zhaoController extends Controller
{
    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function list(Request $request)
   {    
        $id= $request->id;
        session('id',$id);
       $user = xiang::findOrFail($id);

       $photos = DB::table('photos')->where('album_id',$id)->get();
       // dd($photos);
        return view('list',['user'=>$user,'photos'=>$photos]);
   }
    /**
     * Show the form for creating a new resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function dolist(Request $request)
   {
        // $res = $request->album_id;

        $user = new xiang;

         $name =  $user ->name = $request->name;
         $intro = $user ->intro =$request->intro;
           if($request->hasFile('photo')) {
                //创建文件的名字
                $filename = time().rand(10000,99999);
                //获取文件的后缀
                $suffix = $request->file('photo')->getClientOriginalExtension();
                //文件夹
                $dirname = './uploads/';

               
                //文件名
                $file = $filename .'.'. $suffix;

                //移动
                $request->file('photo')->move($dirname,$file);
                 $dirname = trim($dirname,'.');
                //修改图片属性
               $res = $user->img = $dirname.$file;

            }

                //存储数据
            $photo = photo::insert([
                    'album_id'=>$request->album_id,
                    'name'=>$file,
                    'intro'=>$request->intro,
                    'src'=>$res
                ]);
            return back();
   }

    /**
     * Store a newly created resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return \Illuminate\Http\Response
     */
    public function bianji(Request $request, $id)
    {
        $name = $request->name;
        $intro = $request->intro;

        if($request->hasFile('img')) {
            //创建文件的名字
            $filename = time().rand(10000,99999);
            //获取文件的后缀
            $suffix = $request->file('img')->getClientOriginalExtension();
            //文件夹
            $dirname = './uploads/';

           
            //文件名
            $file = $filename .'.'. $suffix;

            //移动
            $request->file('img')->move($dirname,$file);
             $dirname = trim($dirname,'.');
            //修改图片属性
            $res = $dirname.$file;

        }
        $res = DB::table('users')->where('id',$id)->update(['name'=>$name,'intro'=>$intro,'img'=>$res]);
         return redirect('/xiang');
    }

    /**
     * Display the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function shan($id)
    {
         $user = User::findOrFail($id);
       
        if($user->delete()){
            return redirect('/xiang')->with('info','删除成功');
        }else{
            return back()->with('info','删除失败');
        }
    }

    
}
